package com.haibei.view.web;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 轻量持久化，往SharedPreferences存储读取数据
 * 
 * @author liunz
 * 
 */
public class SharePersistentUtils {

	/**
	 * SharedPreferences文件名称
	 */
	public static final String PREFS_NAME = "wuyou";

	/**
	 * 获取SharedPreferences的文件名称
	 * 
	 * @param context
	 * @return
	 */
	private static String getPreferenceName(Context context) {
		return context.getApplicationContext().getPackageName();
	}

	/**
	 * 以键值对的方式存入到SharedPreferences中
	 * 
	 * @param context
	 * @param key
	 *            String型的key
	 * @param value
	 *            String型的value
	 */
	public static void savePerference(Context context, String key, String value) {
		SharedPreferences settings = context.getSharedPreferences(getPreferenceName(context), Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(key, value);
		editor.commit();
	}

	/**
	 * 以键值对的方式存入到SharedPreferences中
	 * 
	 * @param context
	 * @param key
	 *            String型的key
	 * @param value
	 *            int型的value
	 */
	public static void savePerference(Context context, String key, int value) {
		SharedPreferences settings = context.getSharedPreferences(getPreferenceName(context), Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt(key, value);
		editor.commit();
	}

	/**
	 * 以键值对的方式存入到SharedPreferences中
	 * 
	 * @param context
	 * @param key
	 *            String型的key
	 * @param value
	 *            boolean型的value
	 */
	public static void saveBoolean(Context context, String key, boolean value) {
		SharedPreferences settings = context.getSharedPreferences(getPreferenceName(context), Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	/**
	 * 查找SharedPreferences中对应key的值
	 * 
	 * @param context
	 * @param key
	 *            要查找的key
	 * @return 查找的结果，输出String型，默认输出""
	 */
	public static String getPerference(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(getPreferenceName(context), Context.MODE_PRIVATE);
		return settings.getString(key, "");
	}

	/**
	 * 查找SharedPreferences中对应key的值
	 * 
	 * @param context
	 * @param key
	 *            要查找的key
	 * @return 查找的结果，输出Int型，默认输出0
	 */
	public static int getInt(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(getPreferenceName(context), Context.MODE_PRIVATE);
		return settings.getInt(key, 0);
	}

	/**
	 * 查找SharedPreferences中对应key的值
	 * 
	 * @param context
	 * @param key
	 *            要查找的key
	 * @return 查找的结果，输出Boolean型，默认输出false
	 */
	public static boolean getBoolean(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(getPreferenceName(context), Context.MODE_PRIVATE);
		return settings.getBoolean(key, false);
	}

	/**
	 * 删除SharedPreferences中对应key的值
	 * 
	 * @param context
	 * @param key
	 *            要删除的key
	 */
	public static void removePerference(Context context, String key) {
		SharedPreferences settings = context.getSharedPreferences(getPreferenceName(context), Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.remove(key);
		editor.commit();
	}
}
